package annotion.example.hss_fst.baselib.fragments;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.internal.ThemeSingleton;

import java.util.LinkedList;
import java.util.List;

import annotion.example.hss_fst.baselib.R;
import annotion.example.hss_fst.baselib.materiadialog.ChangelogDialog;
import annotion.example.hss_fst.baselib.materiadialog.simplelist.MaterialSimpleListAdapter;
import annotion.example.hss_fst.baselib.materiadialog.simplelist.MaterialSimpleListItem;


/**
 * <b>Project:</b> BaseLib<br>
 * <b>Create Date:</b> 2016/4/29<br>
 * <b>Author:</b> huang saisai <br>
 * <b>Description:</b> <br>
 */
public class MateriaDialogFragment extends ListFragment {
    ArrayAdapter adapter;

    List<String> list;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        list = new LinkedList<>();
        for (int i=0; i<30; i++){
            list.add(String.valueOf(i));
        }
        adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
                                           list);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.setListAdapter(adapter);
        getListView().setOnItemClickListener(itemClickListener);
    }


    AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position){
                case 0://basicNoTitle
                    new MaterialDialog.Builder(getActivity())
                            .content(R.string.shareLocationPrompt)
                            .positiveText(R.string.agree)
                            .negativeText(R.string.disagree)
                            .show();
                    break;
                case 1://basic
                    new MaterialDialog.Builder(getActivity())
                            .title(R.string.useGoogleLocationServices)
                            .content(R.string.useGoogleLocationServicesPrompt)
                            .positiveText(R.string.agree)
                            .negativeText(R.string.disagree)
                            .show();
                    break;
                case 2://showBasicLongContent
                    new MaterialDialog.Builder(getActivity())
                            .title(R.string.useGoogleLocationServices)
                            .content(R.string.loremIpsum)
                            .positiveText(R.string.agree)
                            .negativeText(R.string.disagree)
                            .show();
                    break;
                case 3://basicIcon
                    new MaterialDialog.Builder(getActivity())
                            .iconRes(R.mipmap.ic_launcher)
                            .limitIconToDefaultSize() // limits the displayed icon size to 48dp
                            .title(R.string.useGoogleLocationServices)
                            .content(R.string.useGoogleLocationServicesPrompt)
                            .positiveText(R.string.agree)
                            .negativeText(R.string.disagree)
                            .show();
                    break;
                case 4://stacked
                    new MaterialDialog.Builder(getActivity())
                            .title(R.string.useGoogleLocationServices)
                            .content(R.string.useGoogleLocationServicesPrompt)
                            .positiveText(R.string.speedBoost)
                            .negativeText(R.string.noThanks)
                            .btnStackedGravity(GravityEnum.END)
                            .forceStacking(true)  // this generally should not be forced, but is used for demo purposes
                            .show();
                    break;
                case 5://showNeutral
                    new MaterialDialog.Builder(getActivity())
                            .title(R.string.useGoogleLocationServices)
                            .content(R.string.useGoogleLocationServicesPrompt)
                            .positiveText(R.string.agree)
                            .negativeText(R.string.disagree)
                            .neutralText(R.string.more_info)
                            .show();
                    break;
                case 6://showCallbacks
                    new MaterialDialog.Builder(getActivity())
                            .title(R.string.useGoogleLocationServices)
                            .content(R.string.useGoogleLocationServicesPrompt)
                            .positiveText(R.string.agree)
                            .negativeText(R.string.disagree)
                            .neutralText(R.string.more_info)
                            .onAny(new MaterialDialog.SingleButtonCallback() {
                                @Override
                                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                                    showToast(which.name() + "!");
                                }
                            })
                            .show();

                    break;
                case 7://list
                    new MaterialDialog.Builder(getActivity())
                            .title(R.string.socialNetworks)
                            .items(R.array.socialNetworks)
                            .itemsCallback(new MaterialDialog.ListCallback() {
                                @Override
                                public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
//                                    showToast(which + ": " + text);
                                }
                            })
                            .show();
                    break;
                case 8://listNoTitle
                    new MaterialDialog.Builder(getActivity())
                            .items(R.array.socialNetworks)
                            .itemsCallback(new MaterialDialog.ListCallback() {
                                @Override
                                public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
//                                    showToast(which + ": " + text);
                                }
                            })
                            .show();
                    break;
                case 9://longList
                    new MaterialDialog.Builder(getActivity())
                            .title(R.string.states)
                            .items(R.array.states)
                            .itemsCallback(new MaterialDialog.ListCallback() {
                                @Override
                                public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
//                                    showToast(which + ": " + text);
                                }
                            })
                            .positiveText(android.R.string.cancel)
                            .show();
                    break;
                case 10://longList
                    new MaterialDialog.Builder(getActivity())
                            .title(R.string.states)
                            .items(R.array.states)
                            .itemsCallback(new MaterialDialog.ListCallback() {
                                @Override
                                public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
//                                    showToast(which + ": " + text);
                                }
                            })
                            .positiveText(android.R.string.cancel)
                            .show();
                    break;
                case 11://list_longItems
                    new MaterialDialog.Builder(getActivity())
                            .title(R.string.socialNetworks)
                            .items(R.array.socialNetworks_longItems)
                            .itemsCallback(new MaterialDialog.ListCallback() {
                                @Override
                                public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
//                                    showToast(which + ": " + text);
                                }
                            })
                            .show();
                    break;
                case 12://showSingleChoice
                    new MaterialDialog.Builder(getActivity())
                            .title(R.string.socialNetworks)
                            .items(R.array.socialNetworks)
                            .itemsCallbackSingleChoice(2, new MaterialDialog.ListCallbackSingleChoice() {
                                @Override
                                public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
//                                    showToast(which + ": " + text);
                                    return true; // allow selection
                                }
                            })
                            .positiveText(R.string.more_info)
                            .show();
                    break;
                case 13://showSingleChoiceLongItems
                    new MaterialDialog.Builder(getActivity())
                            .title(R.string.socialNetworks)
                            .items(R.array.socialNetworks_longItems)
                            .itemsCallbackSingleChoice(2, new MaterialDialog.ListCallbackSingleChoice() {
                                @Override
                                public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
//                                    showToast(which + ": " + text);
                                    return true; // allow selection
                                }
                            })
                            .positiveText(R.string.app_name)
                            .show();
                    break;
                case 14://showMultiChoice
                    new MaterialDialog.Builder(getActivity())
                            .title(R.string.socialNetworks)
                            .items(R.array.socialNetworks)
                            .itemsCallbackMultiChoice(new Integer[]{1, 3}, new MaterialDialog.ListCallbackMultiChoice() {
                                @Override
                                public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
                                    StringBuilder str = new StringBuilder();
                                    for (int i = 0; i < which.length; i++) {
                                        if (i > 0) str.append('\n');
                                        str.append(which[i]);
                                        str.append(": ");
                                        str.append(text[i]);
                                    }
//                                    showToast(str.toString());
                                    return true; // allow selection
                                }
                            })
                            .onNeutral(new MaterialDialog.SingleButtonCallback() {
                                @Override
                                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                    dialog.clearSelectedIndices();
                                }
                            })
                            .alwaysCallMultiChoiceCallback()
                            .positiveText(R.string.app_name)
                            .autoDismiss(false)
                            .neutralText(R.string.clear_selection)
                            .show();
                    break;
                case 15://multiChoiceLimited
                    new MaterialDialog.Builder(getActivity())
                            .title(R.string.socialNetworks)
                            .items(R.array.socialNetworks)
                            .itemsCallbackMultiChoice(new Integer[]{1}, new MaterialDialog.ListCallbackMultiChoice() {
                                @Override
                                public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
                                    boolean allowSelection = which.length <= 2; // limit selection to 2, the new selection is included in the which array
                                    if (!allowSelection) {
//                                        showToast(R.string.selection_limit_reached);
                                    }
                                    return allowSelection;
                                }
                            })
                            .positiveText(R.string.dismiss)
                            .alwaysCallMultiChoiceCallback() // the callback will always be called, to check if selection is still allowed
                            .show();
                    break;
                case 16://multiChoice_longItems
                    new MaterialDialog.Builder(getActivity())
                            .title(R.string.socialNetworks)
                            .items(R.array.socialNetworks_longItems)
                            .itemsCallbackMultiChoice(new Integer[]{1, 3}, new MaterialDialog.ListCallbackMultiChoice() {
                                @Override
                                public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
                                    StringBuilder str = new StringBuilder();
                                    for (int i = 0; i < which.length; i++) {
                                        if (i > 0) str.append('\n');
                                        str.append(which[i]);
                                        str.append(": ");
                                        str.append(text[i]);
                                    }
//                                    showToast(str.toString());
                                    return true; // allow selection
                                }
                            })
                            .positiveText(R.string.app_name)
                            .show();
                    break;
                case 17://simpleList
                    final MaterialSimpleListAdapter adapter = new MaterialSimpleListAdapter(getActivity());
                    adapter.add(new MaterialSimpleListItem.Builder(getActivity())
                                        .content("username@gmail.com")
                                        .icon(R.drawable.ic_account_circle)
                                        .backgroundColor(Color.WHITE)
                                        .build());
                    adapter.add(new MaterialSimpleListItem.Builder(getActivity())
                                        .content("user02@gmail.com")
                                        .icon(R.drawable.ic_account_circle)
                                        .backgroundColor(Color.WHITE)
                                        .build());
                    adapter.add(new MaterialSimpleListItem.Builder(getActivity())
                                        .content(R.string.add_account)
                                        .icon(R.drawable.ic_content_add)
                                        .iconPaddingDp(8)
                                        .build());

                    new MaterialDialog.Builder(getActivity())
                            .title(R.string.set_backup)
                            .adapter(adapter, new MaterialDialog.ListCallback() {
                                @Override
                                public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                                    MaterialSimpleListItem item = adapter.getItem(which);
//                                    showToast(item.getContent().toString());
                                }
                            })
                            .show();
                    break;
                case 18://customListItems
                    new MaterialDialog.Builder(getActivity())
                            .title(R.string.socialNetworks)
                            .adapter(new  annotion.example.hss_fst.baselib.materiadialog.simplelist.ButtonItemAdapter(getContext(), R.array.socialNetworks),
                                     new MaterialDialog.ListCallback() {
                                         @Override
                                         public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
//                                             showToast("Clicked item " + which);
                                         }
                                     })
                            .show();
                    break;
                case 19://customView
//                    MaterialDialog dialog = new MaterialDialog.Builder(getActivity())
//                            .title(R.string.googleWifi)
//                            .customView(R.layout.dialog_customview, true)
//                            .positiveText(R.string.connect)
//                            .negativeText(android.R.string.cancel)
//                            .onPositive(new MaterialDialog.SingleButtonCallback() {
//                                @Override
//                                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
////                                    showToast("Password: " + passwordInput.getText().toString());
//                                }
//                            }).build();
//
//                    positiveAction = dialog.getActionButton(DialogAction.POSITIVE);
//                    //noinspection ConstantConditions
//                    passwordInput = (EditText) dialog.getCustomView().findViewById(R.id.password);
//                    passwordInput.addTextChangedListener(new TextWatcher() {
//                        @Override
//                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                        }
//
//                        @Override
//                        public void onTextChanged(CharSequence s, int start, int before, int count) {
////                            positiveAction.setEnabled(s.toString().trim().length() > 0);
//                        }
//
//                        @Override
//                        public void afterTextChanged(Editable s) {
//                        }
//                    });
//
//                    // Toggling the show password CheckBox will mask or unmask the password input EditText
//                    CheckBox checkbox = (CheckBox) dialog.getCustomView().findViewById(R.id.showPassword);
//                    checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                        @Override
//                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                            passwordInput.setInputType(!isChecked ? InputType.TYPE_TEXT_VARIATION_PASSWORD : InputType.TYPE_CLASS_TEXT);
//                            passwordInput.setTransformationMethod(!isChecked ? PasswordTransformationMethod.getInstance() : null);
//                        }
//                    });
//
//                    int widgetColor = ThemeSingleton.get().widgetColor;
//                    MDTintHelper.setTint(checkbox,
//                                         widgetColor == 0 ? ContextCompat.getColor(this, R.color.material_teal_a400) : widgetColor);
//
//                    MDTintHelper.setTint(passwordInput,
//                                         widgetColor == 0 ? ContextCompat.getColor(this, R.color.material_teal_a400) : widgetColor);
//
//                    dialog.show();
//                    positiveAction.setEnabled(false); // disabled by default
                    break;
                case 20://customView_webView
                    int accentColor = ThemeSingleton.get().widgetColor;
                    if (accentColor == 0)
                        accentColor = ContextCompat.getColor(getActivity(), R.color.material_teal_a400);
                    ChangelogDialog.create(false, accentColor)
                                   .show(getChildFragmentManager(), "changelog");
                    break;
                case 21://colorChooser_primary
//                    new ColorChooserDialog.Builder(this, R.string.color_palette)
//                            .titleSub(R.string.colors)
//                            .preselect(primaryPreselect)
//                            .show();
                    break;//
                case 22://colorChooser_accent
//                    new ColorChooserDialog.Builder(this, R.string.color_palette)
//                            .titleSub(R.string.colors)
//                            .accentMode(true)
//                            .preselect(accentPreselect)
//                            .show();
                    break;
                case 23://colorChooser_customColors
//                    int[][] subColors = new int[][]{
//                            new int[]{Color.parseColor("#EF5350"), Color.parseColor("#F44336"), Color.parseColor("#E53935")},
//                            new int[]{Color.parseColor("#EC407A"), Color.parseColor("#E91E63"), Color.parseColor("#D81B60")},
//                            new int[]{Color.parseColor("#AB47BC"), Color.parseColor("#9C27B0"), Color.parseColor("#8E24AA")},
//                            new int[]{Color.parseColor("#7E57C2"), Color.parseColor("#673AB7"), Color.parseColor("#5E35B1")},
//                            new int[]{Color.parseColor("#5C6BC0"), Color.parseColor("#3F51B5"), Color.parseColor("#3949AB")},
//                            new int[]{Color.parseColor("#42A5F5"), Color.parseColor("#2196F3"), Color.parseColor("#1E88E5")}
//                    };
//
//                    new ColorChooserDialog.Builder(this, R.string.color_palette)
//                            .titleSub(R.string.colors)
//                            .preselect(primaryPreselect)
//                            .customColors(R.array.custom_colors, subColors)
//                            .show();
                    break;
                case 24://colorChooser_customColorsNoSub
//                    new ColorChooserDialog.Builder(this, R.string.color_palette)
//                            .titleSub(R.string.colors)
//                            .preselect(primaryPreselect)
//                            .customColors(R.array.custom_colors, null)
//                            .show();
                    break;
                case 25://themed
//                    new MaterialDialog.Builder(this)
//                            .title(R.string.useGoogleLocationServices)
//                            .content(R.string.useGoogleLocationServicesPrompt)
//                            .positiveText(R.string.agree)
//                            .negativeText(R.string.disagree)
//                            .positiveColorRes(R.color.material_red_400)
//                            .negativeColorRes(R.color.material_red_400)
//                            .titleGravity(GravityEnum.CENTER)
//                            .titleColorRes(R.color.material_red_400)
//                            .contentColorRes(android.R.color.white)
//                            .backgroundColorRes(R.color.material_blue_grey_800)
//                            .dividerColorRes(R.color.material_teal_a400)
//                            .btnSelector(R.drawable.md_btn_selector_custom, DialogAction.POSITIVE)
//                            .positiveColor(Color.WHITE)
//                            .negativeColorAttr(android.R.attr.textColorSecondaryInverse)
//                            .theme(Theme.DARK)
//                            .show();
                    break;
                case 26://showCancelDismiss
                    new MaterialDialog.Builder(getActivity())
                            .title(R.string.useGoogleLocationServices)
                            .content(R.string.useGoogleLocationServicesPrompt)
                            .positiveText(R.string.agree)
                            .negativeText(R.string.disagree)
                            .neutralText(R.string.more_info)
                            .showListener(new DialogInterface.OnShowListener() {
                                @Override
                                public void onShow(DialogInterface dialog) {
//                                    showToast("onShow");
                                }
                            })
                            .cancelListener(new DialogInterface.OnCancelListener() {
                                @Override
                                public void onCancel(DialogInterface dialog) {
//                                    showToast("onCancel");
                                }
                            })
                            .dismissListener(new DialogInterface.OnDismissListener() {
                                @Override
                                public void onDismiss(DialogInterface dialog) {
//                                    showToast("onDismiss");
                                }
                            })
                            .show();
                    break;
                case 27://file_chooser
//                    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) !=
//                        PackageManager.PERMISSION_GRANTED) {
//                        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_RC);
//                        return;
//                    }
//                    new FileChooserDialog.Builder(getActivity())
//                            .show();
                    break;
            }
        }
    };
}
